package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.EmpBenefitsRemindListDTO;
import com.oa.admin.entity.EmpBenefitsRemind;
import com.oa.admin.service.IEmpBenefitsRemindService;
import com.oa.admin.vo.EmpBenefitsRemindDetailsVO;
import com.oa.admin.vo.EmpBenefitsRemindListVO;
import com.oa.core.utils.StringUtil;
import com.oa.system.entity.User;
import com.oa.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/empBenefitsRemind")
@Api(description = "员工福利提醒模块")
public class EmpBenefitsRemindController extends BaseController {

    private final IEmpBenefitsRemindService empBenefitsRemindService;

    @Autowired
    private IUserService userService;

    @Autowired
    public EmpBenefitsRemindController(IEmpBenefitsRemindService empBenefitsRemindService) {
        this.empBenefitsRemindService = empBenefitsRemindService;
    }

    /**
     * 根据Id查询
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public Resp<EmpBenefitsRemindListVO> list(EmpBenefitsRemindListDTO empBenefitsRemindListDTO) {
        return new Resp().success(empBenefitsRemindService.findList(empBenefitsRemindListDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<EmpBenefitsRemindDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        EmpBenefitsRemindDetailsVO empBenefitsRemindDetailsVO = new EmpBenefitsRemindDetailsVO();

        EmpBenefitsRemind empBenefitsReminds = empBenefitsRemindService.findEmpBenefitsRemindById(id);
        BeanUtils.copyProperties(empBenefitsReminds, empBenefitsRemindDetailsVO);
        if(StringUtils.isNotEmpty(empBenefitsReminds.getAlertOthers())){
            String[] userIds = empBenefitsReminds.getAlertOthers().split(",");
            List<User> users = new ArrayList<>();
            for (String userId : userIds) {
                User user = userService.selectById(userId);
                users.add(user);
            }
            empBenefitsRemindDetailsVO.setAlertOthers(users);
        }
        return new Resp().success(empBenefitsRemindDetailsVO);
    }


    /**
     * 新增
     *
     * @param empBenefitsRemind EmpBenefitsRemind
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "员工福利提醒对象") @RequestBody(required = false) EmpBenefitsRemind empBenefitsRemind) {
        if (empBenefitsRemindService.insert(empBenefitsRemind)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param empBenefitsRemind EmpBenefitsRemind
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "员工福利提醒对象") @RequestBody(required = false) EmpBenefitsRemind empBenefitsRemind) {
        if (empBenefitsRemindService.updateById(empBenefitsRemind)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        EmpBenefitsRemind empBenefitsRemind = empBenefitsRemindService.selectById(id);
        if (empBenefitsRemind != null && empBenefitsRemind.getIsDeleted() == 2) {
            empBenefitsRemind.setIsDeleted(1);
            if (empBenefitsRemindService.updateById(empBenefitsRemind)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
